﻿using System;
using UI;
using UnityEngine;

public class AreaTrigger : MonoBehaviour
{
    [Header("前置条件")]
    public NeedItemInfo[] needInfoArr;

    private bool m_registed;

    private void OnEnable()
    {
        if (needInfoArr.All(false))
        {
            gameObject.SetActive(false);
            return;
        }
        m_registed = true;
        InventoryManager.OnGetItem += OnGetItem;
        InventoryManager.OnUseItem += OnUseItem;
    }

    private void OnDisable()
    {
        if (m_registed)
        {
            m_registed = false;
            InventoryManager.OnGetItem -= OnGetItem;
            InventoryManager.OnUseItem -= OnUseItem;
        }
    }

    private void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.layer == Layers.Player)
        {
            needInfoArr.All(true);
        }
    }

    //回调
    private void OnGetItem(EItemType type)
    {
        if (needInfoArr.All(false))
        {
            gameObject.SetActive(false);
        }
    }
    private void OnUseItem(EItemType type)
    {
        if (needInfoArr.All(false))
        {
            gameObject.SetActive(false);
        }
    }
}